package com.coffee.admin.service.platform.impl;

import com.coffee.admin.api.platform.query.PlatformBaseDeployQuery;
import com.coffee.admin.api.platform.rep.PlatformBaseDeployListRep;
import com.coffee.admin.api.platform.rep.PlatformBaseDeploySonRep;
import com.coffee.admin.api.platform.req.PlatformBaseDeployAddReq;
import com.coffee.admin.api.platform.req.PlatformBaseDeployAddSonReq;
import com.coffee.admin.api.platform.req.PlatformBaseDeployUpdateReq;
import com.coffee.admin.api.platform.result.PlatformBaseDeployResult;
import com.coffee.admin.api.platform.result.PlatformPermissionResult;
import com.coffee.admin.api.platform.service.PlatformBaseDeployService;
import com.coffee.admin.service.platform.bean.PlatformBaseDeploy;
import com.coffee.admin.service.platform.bean.PlatformPermission;
import com.coffee.admin.service.platform.dao.PlatformBaseDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class PlatformBaseDeployServiceImpl implements PlatformBaseDeployService {
    @Resource
    private PlatformBaseDeployDao platformBaseDeployDao;

    @Override
    public Page<PlatformBaseDeployListRep> getPager(PlatformBaseDeployQuery param, PagerInfo pr) {
        Page<PlatformBaseDeployListRep> pager = new Page<PlatformBaseDeployListRep>(pr.getStart(), pr.getPageSize());
        Integer count = platformBaseDeployDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<PlatformBaseDeploy> list = platformBaseDeployDao.page(param, pr.getStart(), pr.getPageSize());
            List<PlatformBaseDeployListRep> platformBaseDeployListReps = DTOUtils.convertList(list, PlatformBaseDeployListRep.class);
            PlatformBaseDeployQuery sonParam = new PlatformBaseDeployQuery();
            for (PlatformBaseDeployListRep platformBaseDeployListRep : platformBaseDeployListReps) {
                sonParam.setPid(platformBaseDeployListRep.getId());
                List<PlatformBaseDeploy> sonPlatformBaseDeploy = platformBaseDeployDao.list(sonParam);
                platformBaseDeployListRep.setPlatformBaseDeploySonReps(DTOUtils.convertList(sonPlatformBaseDeploy, PlatformBaseDeploySonRep.class));
            }
            pager.setData(platformBaseDeployListReps);
        }
        return pager;
    }

    @Override
    public Integer count(PlatformBaseDeployQuery param) {
        Integer count = platformBaseDeployDao.count(param);
        return count;
    }

    @Override
    public PlatformBaseDeployResult getDetail(Integer id) {
        PlatformBaseDeploy item = platformBaseDeployDao.findById(id);
        if (item == null) {
            return null;
        }
        PlatformBaseDeployResult result = new PlatformBaseDeployResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<PlatformBaseDeployResult> getList(PlatformBaseDeployQuery param, PagerInfo pr) {
        List<PlatformBaseDeploy> list = platformBaseDeployDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, PlatformBaseDeployResult.class);
    }

    @Override
    public List<PlatformBaseDeployResult> getList(PlatformBaseDeployQuery param) {
        List<PlatformBaseDeploy> list = platformBaseDeployDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PlatformBaseDeployResult.class);
    }

    @Override
    public List<PlatformBaseDeployResult> getList() {
        PlatformBaseDeployQuery param = new PlatformBaseDeployQuery();
        List<PlatformBaseDeploy> list = platformBaseDeployDao.page(param, 0, Integer.MAX_VALUE);
        Map<Integer, List<PlatformBaseDeploy>> integerListMap = list.stream().collect(Collectors.groupingBy(PlatformBaseDeploy::getPid));
        List<PlatformBaseDeploy> platformBaseDeploys = integerListMap.get(0);
        List<PlatformBaseDeployResult> platformBaseDeployResults = DTOUtils.convertList(platformBaseDeploys, PlatformBaseDeployResult.class);
        recursion(integerListMap, platformBaseDeployResults);
        return platformBaseDeployResults;
    }

    public void recursion(Map<Integer, List<PlatformBaseDeploy>> permissionMap, List<PlatformBaseDeployResult> adminMenuResults) {
        for (PlatformBaseDeployResult adminMenuResult : adminMenuResults) {
            Integer id = adminMenuResult.getId();
            List<PlatformBaseDeploy> adminMenus = permissionMap.get(id);
            if (CollectionUtils.isNotEmpty(adminMenus)) {
                List<PlatformBaseDeployResult> submenuList = DTOUtils.convertList(adminMenus, PlatformBaseDeployResult.class);
                recursion(permissionMap, submenuList);
                adminMenuResult.setChildList(submenuList);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        platformBaseDeployDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(PlatformBaseDeployAddReq platformBaseDeployAddReq) throws BusinessException {
        if (platformBaseDeployAddReq.getId() != null) {
            PlatformBaseDeploy platformBaseDeploy = platformBaseDeployDao.findById(platformBaseDeployAddReq.getId());
            if (platformBaseDeploy == null || 0 != platformBaseDeploy.getPid()) {
                throw BusinessException.build(ExceptionEnum.ERROR_1600);
            }
            PlatformBaseDeployAddSonReq platformBaseDeployAddSonReqs = platformBaseDeployAddReq.getPlatformBaseDeployAddSonReqs();
            PlatformBaseDeploy platformBaseDeploy1 = new PlatformBaseDeploy();
            BeanUtilExt.copyProperties(platformBaseDeploy1, platformBaseDeployAddSonReqs);
            platformBaseDeploy1.setPid(platformBaseDeploy.getId());
            platformBaseDeployDao.insert(platformBaseDeploy1);
        } else {
            PlatformBaseDeploy item = new PlatformBaseDeploy();
            PlatformBaseDeployQuery platformBaseDeployQuery=new PlatformBaseDeployQuery();
            platformBaseDeployQuery.setName(platformBaseDeployAddReq.getName());
            PlatformBaseDeploy first = platformBaseDeployDao.findFirst(platformBaseDeployQuery);
            if (first!=null){
                throw BusinessException.build(ExceptionEnum.ERROR_1601);
            }
            BeanUtilExt.copyProperties(item, platformBaseDeployAddReq);
            item.setPid(0);
            platformBaseDeployDao.insert(item);
            PlatformBaseDeployAddSonReq platformBaseDeployAddSonReqs = platformBaseDeployAddReq.getPlatformBaseDeployAddSonReqs();
            PlatformBaseDeploy platformBaseDeploy1 = new PlatformBaseDeploy();
            BeanUtilExt.copyProperties(platformBaseDeploy1, platformBaseDeployAddSonReqs);
            platformBaseDeploy1.setPid(item.getId());
            platformBaseDeployDao.insert(platformBaseDeploy1);
        }
    }

    @Override
    public void update(PlatformBaseDeployUpdateReq platformBaseDeployUpdateReq) throws BusinessException {
        PlatformBaseDeploy item = new PlatformBaseDeploy();
        BeanUtilExt.copyProperties(item, platformBaseDeployUpdateReq);
        platformBaseDeployDao.updateById(item);
    }


    @Override
    public Map<Integer, PlatformBaseDeployResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, PlatformBaseDeployResult> map = new HashMap<Integer, PlatformBaseDeployResult>();
        List<PlatformBaseDeploy> list = platformBaseDeployDao.findByIds(StringUtil.setToList(idSet));
        List<PlatformBaseDeployResult> resultList = DTOUtils.convertList(list, PlatformBaseDeployResult.class);
        for (PlatformBaseDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, PlatformBaseDeployResult> getItemMap() {
        Map<Integer, PlatformBaseDeployResult> map = new HashMap<Integer, PlatformBaseDeployResult>();
        PlatformBaseDeployQuery param = new PlatformBaseDeployQuery();
        List<PlatformBaseDeploy> list = platformBaseDeployDao.page(param, 0, Integer.MAX_VALUE);
        List<PlatformBaseDeployResult> resultList = DTOUtils.convertList(list, PlatformBaseDeployResult.class);
        for (PlatformBaseDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
